package handler;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import settings.exception.LoginException;
import util.JsonUtil;
import workbench.exception.*;

@ControllerAdvice
public class GlobalExceptionHandler implements HandlerInterceptor {
    @ExceptionHandler(value= LoginException.class)
    @ResponseBody
    public String LoginException(LoginException ex){
        return "{\"status\":false,\"errorMessage\":\""+ex.getMessage()+"\"}";
    }
    @ExceptionHandler(value= DeleteException.class)
    @ResponseBody
    public String DeleteException(DeleteException ex){
        return JsonUtil.ordinaryValueToJson("success","false");
    }
    @ExceptionHandler(value= UpdateException.class)
    @ResponseBody
    public String UpdateException(DeleteException ex){
        return JsonUtil.ordinaryValueToJson("success","false");
    }
    @ExceptionHandler(value= InsertClueException.class)
    @ResponseBody
    public String InsertClueException(InsertClueException ex){
        return JsonUtil.ordinaryValueToJson("success","false");
    }
    @ExceptionHandler(value= SaveRemarkException .class)
    @ResponseBody
    public String SaveRemarkException(SaveRemarkException  ex){
        return JsonUtil.ordinaryValueToJson("success","false");
    }
    @ExceptionHandler(value= ConvertException.class)
    public ModelAndView ConvertException(ConvertException ex){
        ModelAndView mv=new ModelAndView();
        mv.addObject("errorMessage",ex.getMessage());
        mv.setViewName("error");
        return mv;
    }
    @ExceptionHandler(value= AddTranException.class)
    public ModelAndView AddTranException(AddTranException ex){
        ModelAndView mv=new ModelAndView();
        mv.addObject("errorMessage",ex.getMessage());
        mv.setViewName("error");
        return mv;
    }
    @ExceptionHandler(value= CreateTranHistoryException.class)
    public ModelAndView CreateTranHistoryException(CreateTranHistoryException ex){
        ModelAndView mv=new ModelAndView();
        mv.addObject("errorMessage",ex.getMessage());
        mv.setViewName("error");
        return mv;
    }
    @ExceptionHandler(value= Exception.class)
    public ModelAndView accidentalException(Exception ex){
        ModelAndView mv=new ModelAndView();
        mv.addObject("errorMessage",ex.getMessage());
        mv.setViewName("error");
        return mv;
    }
}
